package main

import (
	`flag`
	`fmt`
	`io/ioutil`
	`strings`

	`tools/src/utils`
	`tools/src/utils/e1`
)

func main() {
	var fromFile = flag.String("f", "shell/out_example", "from fromFile")
	var toFile = flag.String("e", "redis-pre-checkpoint-compare.xlsx", "to fromFile")
	var sheetName = flag.String("s", "redis-pre-cp-compare", "sheet name")
	flag.Parse()

	content, err := readFile(*fromFile)
	if err != nil {
		fmt.Println("Read file fail, detail:", err)
		return
	}

	parsedContent := e1.ParseContent(content)
	if !strings.HasSuffix(*toFile, ".xlsx") {
		*toFile = *toFile + ".xlsx"
	}

	wb, err := e1.InitExcel(*toFile, *sheetName)
	if err != nil {
		fmt.Println(err)
		return
	}
	defer func() {
		if err := wb.Save(*toFile); err != nil {
			fmt.Println(err)
		}
	}()

	sh := wb.Sheet[*sheetName]
	e1.SetRows(sh, parsedContent)
}

func readFile(filename string) (*string, error) {
	buffer, err := ioutil.ReadFile(filename)
	if err != nil {
		return nil, err
	}

	content := utils.UnsafeBytes2String(buffer)
	return content, nil
}
